(function (){
    const express = require('express');
    const path = require('path');
    const cookieParser = require('cookie-parser');
    const bodyParser = require('body-parser');
    const session    = require('express-session');
    const ejs = require('ejs');
    const connect = require('connect');

    const routers = require('./routers.js');

    const app = express();

    app.use(function(req, res, next) {
        res.setHeader('Access-Control-Allow-Origin', '*');
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
        next();
    });

    app.use(bodyParser.urlencoded({
        extended: true
    }));
    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.engine('.html', ejs.__express);
    app.set('view engine', 'html');

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser('Clcion'));
    app.use(session({
        secret: 'Clcion',
        name:'Clcion',
        store: '',
        resave:false,
        saveUninitialized: true
    }));
    app.use(function (req, res, next) {
        res.locals.user = req.session.user;
        req.session.cookie.expires = new Date(Date.now() + 86400000);
        req.session.cookie.maxAge = 86400000;
        next();
    });
    app.use(express.static(path.join(__dirname, 'public')));

    //时间组件默认设置
    app.locals.moment = require('moment');
    routers(app);

    module.exports = app;
}).call(this);
